package server

import (
	"common/database/config"
	"context"
	"errors"
	"num_srv/internal/models/dao"
	"num_srv/internal/models/service"
	pb "num_srv/proto"
	"time"
)

type Server struct {
	pb.UnimplementedNumServer
}

func (c *Server) Create(ctx context.Context, request *pb.CreateRequest) (*pb.CreateResponse, error) {
	num := &dao.Num{
		IolUserId: int(request.IolUserId),
		DoctorId:  int(request.DoctorId),
		Times:     time.Now().String(),
		Type:      int(request.Type),
		Why:       request.Why,
		Methods:   request.Methods,
	}
	err := config.DB.Create(&num).Error
	if err != nil {
		return nil, errors.New("预约挂号失败")
	}
	return &pb.CreateResponse{Id: int64(num.ID)}, nil
}

func (c *Server) List(ctx context.Context, request *pb.ListRequest) (*pb.ListResponse, error) {
	list, s := service.List(request)
	if s != "" {
		return nil, errors.New(s)
	}
	return &pb.ListResponse{NumMessage: list}, nil
}

func (c *Server) Update(ctx context.Context, request *pb.UpdateRequest) (*pb.UpdateResponse, error) {
	num := dao.Num{
		IolUserId: int(request.IolUserId),
		DoctorId:  int(request.DoctorId),
		Times:     time.Now().String(),
		Type:      int(request.Type),
		Why:       request.Why,
		Methods:   request.Methods,
	}

	err := config.DB.Model(&dao.Num{}).Where("id=?", request.NumId).Updates(&num).Error

	if err != nil {
		return nil, errors.New("信息修改失败")
	}

	return &pb.UpdateResponse{}, nil
}
